home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / libs / ttengine.lha / ttengine-4.1 / Examples / Background / background.c < prev    next >
C/C++ Source or Header  |  2002-09-29  |  8KB  |  239 lines

  1. /* test ttrender */
  2.  
  3. #define __NOLIBBASE__
  4.  
  5. #include <proto/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/ttengine.h>
  10. #include <proto/asl.h>
  11. #include <proto/datatypes.h>
  12. #include <datatypes/pictureclass.h>
  13.  
  14. #include <libraries/ttengine.h>
  15.  
  16. extern struct Library *SysBase, *DOSBase;
  17.  
  18. struct Library *TTEngineBase, *IntuitionBase, *GfxBase, *AslBase, *DataTypesBase;
  19. struct BitMap *PicBitMap;
  20. Object *Picture;
  21.  
  22. /*----------------------------------------------------------------------------------------------------*/
  23.  
  24. static STRPTR get_font_name(struct Library *AslBase)
  25.   {
  26.     struct FileRequester *freq;
  27.     STRPTR name = NULL;
  28.  
  29.     if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
  30.       {
  31.         if (AslRequestTags(freq,
  32.           ASLFR_TitleText, (ULONG)"Select TrueType font",
  33.           ASLFR_InitialDrawer, (ULONG)"FONTS:",
  34.           ASLFR_DoPatterns, TRUE,
  35.           ASLFR_InitialPattern, (ULONG)"#?.ttf",
  36.           ASLFR_RejectIcons, TRUE,
  37.           TAG_END))
  38.           {
  39.             ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;
  40.  
  41.             if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
  42.               {
  43.                 strncpy(name, freq->fr_Drawer, namelen);
  44.                 AddPart(name, freq->fr_File, namelen);
  45.               }
  46.           }
  47.         FreeAslRequest(freq);
  48.       }
  49.     return name;
  50.   }
  51.  
  52. /*----------------------------------------------------------------------------------------------------*/
  53.  
  54. static VOID free_font_name(STRPTR name)
  55.   {
  56.     if (name) FreeVec(name);
  57.   }
  58.  
  59. /*----------------------------------------------------------------------------------------------------*/
  60.  
  61. VOID ctext (struct Window *w, STRPTR text, ULONG y)
  62.   {
  63.     UWORD tl, bl;
  64.     struct RastPort *rp = w->RPort;
  65.  
  66.     tl = strlen(text);
  67.     bl = TT_TextLength(rp, text, tl);
  68.     Move(rp, ((448 - bl) >> 1) + w->BorderLeft, y);
  69.     TT_Text(rp, text, tl);
  70.   }
  71.  
  72. /*----------------------------------------------------------------------------------------------------*/
  73.  
  74. BOOL init(VOID)
  75.   {
  76.     if (!(GfxBase = OpenLibrary("graphics.library", 39))) return FALSE;
  77.     if (!(IntuitionBase = OpenLibrary("intuition.library", 39))) return FALSE;
  78.     if (!(AslBase = OpenLibrary("asl.library", 38))) return FALSE;
  79.     if (!(TTEngineBase = OpenLibrary("ttengine.library", 4))) return FALSE;
  80.     if (!(DataTypesBase = OpenLibrary("datatypes.library", 39))) return FALSE;
  81.     return TRUE;
  82.   }
  83.  
  84. /*----------------------------------------------------------------------------------------------------*/
  85.  
  86. VOID cleanup(VOID)
  87.   {
  88.     if (DataTypesBase) CloseLibrary(DataTypesBase);
  89.     if (TTEngineBase) CloseLibrary(TTEngineBase);
  90.     if (AslBase) CloseLibrary(AslBase);
  91.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  92.     if (GfxBase) CloseLibrary(GfxBase);
  93.     return;
  94.   }
  95.  
  96. /*----------------------------------------------------------------------------------------------------*/
  97.  
  98. VOID load_picture(struct Window *w)
  99.   {
  100.     if (Picture = NewDTObject("PROGDIR:picture",
  101.       DTA_GroupID,    GID_PICTURE,
  102.       PDTA_Remap,     TRUE,
  103.       PDTA_DestMode,  PMODE_V43,
  104.       PDTA_Screen,    (ULONG)w->WScreen,
  105.       TAG_END))
  106.       {
  107.         DoDTMethod(Picture, NULL, NULL, DTM_PROCLAYOUT, NULL, DTSIF_NEWSIZE);
  108.         GetDTAttrs(Picture, PDTA_DestBitMap, (ULONG)&PicBitMap, TAG_END);
  109.       }
  110.     return;
  111.   }
  112.  
  113. /*----------------------------------------------------------------------------------------------------*/
  114.  
  115. int Main (void)
  116.   {
  117.     struct Window *win;
  118.     STRPTR fontname;
  119.     APTR font;
  120.  
  121.     if (init())
  122.       {
  123.         if (fontname = get_font_name(AslBase))
  124.           {
  125.             if (win = OpenWindowTags(NULL,
  126.               WA_Top, 25,
  127.               WA_Left, 0,
  128.               WA_InnerWidth, 448,
  129.               WA_InnerHeight, 262,
  130.               WA_CloseGadget, TRUE,
  131.               WA_DragBar, TRUE,
  132.               WA_DepthGadget, TRUE,
  133.               WA_Activate, TRUE,
  134.               WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_INTUITICKS,
  135.               WA_Title, (ULONG)"TTEngine font picture demo",
  136.               TAG_END))
  137.               {
  138.                 ULONG sigmask, signals, pen;
  139.                 BOOL running = TRUE;
  140.                 struct RastPort *rp = win->RPort;
  141.                 WORD cnt = 0;
  142.                 ULONG r = 0, g = 0x7FFFFFFF, b = 0xFFFFFFFF, icl;
  143.  
  144.                 load_picture(win);
  145.  
  146.                 if (font = TT_OpenFont(
  147.                   TT_FontFile, (ULONG)fontname,
  148.                   TT_FontSize, 26,
  149.                 TAG_END))
  150.                   {
  151.                     SetDrMd(rp, JAM1);
  152.                     TT_SetFont(rp, font);
  153.  
  154.                     sigmask = SIGBREAKF_CTRL_C | (1 << win->UserPort->mp_SigBit);
  155.                     while (running)
  156.                       {
  157.                         signals = Wait(sigmask);
  158.                         if (signals & SIGBREAKF_CTRL_C) running = FALSE;
  159.                         if (signals & (1 << win->UserPort->mp_SigBit))
  160.                           {
  161.                             struct IntuiMessage *imsg;
  162.  
  163.                             while (imsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  164.                               {
  165.                                 icl = imsg->Class;
  166.                                 ReplyMsg((struct Message*)imsg);
  167.                                 if (icl == IDCMP_CLOSEWINDOW) running = FALSE;
  168.                                 if (icl == IDCMP_INTUITICKS)
  169.                                   {
  170.                                     if (++cnt > 9)
  171.                                       {
  172.                                         ULONG pen;
  173.  
  174.                                         if (PicBitMap)
  175.                                           BltBitMapRastPort(PicBitMap, 0, 0, rp, win->BorderLeft, win->BorderTop, 448, 262, 0xC0);
  176.  
  177.                                         cnt = 0;
  178.                                         r += 0x0817736F;
  179.                                         g += 0x0A27E836;
  180.                                         b += 0x0C3A47D4;
  181.  
  182.                                         pen = ObtainBestPen(win->WScreen->ViewPort.ColorMap, r, g, b, TAG_END);
  183.                                         SetAPen(rp, pen);
  184.  
  185.                                         TT_SetAttrs(rp,
  186.                                           TT_Window, (ULONG)win,
  187.                                           TT_Antialias, TT_Antialias_On,
  188.                                           TT_Transparency, 0,
  189.                                         TAG_END);
  190.  
  191.                                         ctext(win, "You can see here how the text is", 40);
  192.                                         ctext(win, "smoothed on background picture.", 66);
  193.  
  194.                                         TT_SetAttrs(rp,
  195.                                           TT_Antialias, TT_Antialias_Off,
  196.                                         TAG_END);
  197.  
  198.                                         ctext(win, "This text is not smoothed", 102);
  199.                                         ctext(win, "The difference is clearly", 128);
  200.                                         ctext(win, "visible.", 154);
  201.  
  202.                                         TT_SetAttrs(rp,
  203.                                           TT_Antialias, TT_Antialias_On,
  204.                                           TT_Transparency, 64,
  205.                                         TAG_END);
  206.  
  207.                                         ctext(win, "transparent text (75%)", 190);
  208.  
  209.                                         TT_SetAttrs(rp,
  210.                                           TT_Transparency, 128,
  211.                                         TAG_END);
  212.  
  213.                                         ctext(win, "transparent text (50%)", 216);
  214.  
  215.                                         TT_SetAttrs(rp,
  216.                                           TT_Transparency, 192,
  217.                                         TAG_END);
  218.  
  219.                                         ctext(win, "transparent text (25%)", 242);
  220.  
  221.                                         ReleasePen(win->WScreen->ViewPort.ColorMap, pen);
  222.                                       }
  223.                                   }
  224.                               }
  225.                           }
  226.                       }
  227.                     TT_CloseFont(font);
  228.                   }
  229.                 else PutStr("Font open failed.\n");
  230.  
  231.                 CloseWindow(win);
  232.               }
  233.             free_font_name(fontname);
  234.           }
  235.       }
  236.     cleanup();
  237.     return 0;
  238.   }
  239.